

package cn.shengchao.service;

import cn.shengchao.examstar.me.collect.domain.CourseCollect;
import cn.shengchao.examstar.me.collect.ohs.local.appservices.CollectAppService;
import cn.shengchao.examstar.me.course.ohs.local.appervices.MyCourseAppService;
import cn.shengchao.examstar.product.ohs.local.appservice.CourseService;
import cn.shengchao.examstar.product.ohs.local.message.CourseListDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class CourseAppService {
    @Autowired
    private CourseService courseService;

    @Autowired
    private CollectAppService collectAppService;

    @Autowired
    private MyCourseAppService myCourseAppService;

    public List<CourseListDto> getCourseList(String name, String specifications, Long userId, List<Long> ids) {
        List<CourseListDto> courseListDtoList = courseService.getCourseList(name, specifications,ids);
        if (userId != -1) {
            //获取收藏
            List<CourseCollect> collect = collectAppService.getCollect(userId);
            courseListDtoList.forEach(courseListDto -> {
                courseListDto.setCollect(collect.stream()
                        .anyMatch(courseCollect -> courseCollect.getCourseId().equals(courseListDto.getId())));
            });
            //获取课程解锁数量(课程id/知识点数量)
            Map<Long, Set<Long>> courseKnowledgeSumGroup = myCourseAppService.getMyCourseKnowledgeSum(userId);
            courseListDtoList.forEach(courseListDto -> {
                Long courseId = courseListDto.getId();
                long courseKnowledgeSum = courseKnowledgeSumGroup.getOrDefault(courseId, Set.of()).size();
                courseListDto.setUnLockKnowledgeNodeNum(courseKnowledgeSum);
            });
        }
        return courseListDtoList;
    }
}
